package com.example.workflow.listener;

import com.example.workflow.pojo.UserInfo;
import com.example.workflow.service.UserInfoService;
import com.example.workflow.service.impl.UserInfoServiceImpl;
import com.example.workflow.util.MailUtils;
import lombok.extern.slf4j.Slf4j;
import org.camunda.bpm.engine.RuntimeService;
import org.camunda.bpm.engine.TaskService;
import org.camunda.bpm.engine.delegate.DelegateExecution;
import org.camunda.bpm.engine.delegate.JavaDelegate;
import org.camunda.bpm.engine.task.Task;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.*;

@Slf4j
@Service("timeOutRemindDelegateListener")
public class TimeOutRemindDelegateListener implements JavaDelegate {
    @Autowired
    private UserInfoService userInfoService;

    @Autowired
    private TaskService taskService;

    @Autowired
    private RuntimeService runtimeService;

    @Autowired
    private MailUtils mailUtils;


    @SuppressWarnings("LogStatementNotGuardedByLogCondition")
    @Override
    @Transactional(rollbackFor = Exception.class)
    public void execute(DelegateExecution delegateExecution) throws Exception {
        log.info("TimeOut RemindDelegateListener start, execution: {}", delegateExecution);
        // 获取当前节点的所有待办任务
        List<Task> taskList = taskService.createTaskQuery()
                .processInstanceId(delegateExecution.getProcessInstanceId())
                .list();
//        Set<String> users = new HashSet<>();
        Task task = taskList.get(0);
        Map<String,Object> variables = runtimeService.getVariables(task.getExecutionId());
//        log.info("variables,  " + variables);
        log.info("ActivityName = {}", delegateExecution.getCurrentActivityName());
        if(delegateExecution.getCurrentActivityName().equals("超时提醒1")) {
            log.info("一级审批超时提醒   ,{}",variables.get("cityUsers"));
            remindTimeout((Set<String>) variables.get("cityUsers"));
        } else if(delegateExecution.getCurrentActivityName().equals("超时提醒2")) {
            log.info("二级审批超时提醒   ,{}",variables.get("provinceUsers"));
            remindTimeout((Set<String>) variables.get("provinceUsers"));
        } else if(delegateExecution.getCurrentActivityName().equals("超时提醒3")) {
            log.info("操作人员超时提醒   ,{}",variables.get("operatorUsers"));
            remindTimeout((Set<String>) variables.get("operatorUsers"));
        } else {
            log.info("协助部门超时提醒   ,{}",variables.get("helpUsers"));
//            TODO
            remindTimeout((Set<String>) variables.get("helpUsers"));
        }
    }


    private void remindTimeout(Set<String> userIds) {
        List<Integer> ids = new ArrayList<>();
        for(String userId : userIds) {
            ids.add(Integer.parseInt(userId));
        }
        List<UserInfo> list = userInfoService.getUserInfoByIds(ids);
        for (UserInfo userInfo : list) {
            String toEmail = userInfo.getEmail();
//            mailUtils.sendMail(toEmail,null,null,"超时提醒","尊敬的用户，您好：\n需要您处理的工单即将超时，请及时登录系统处理！");
        }
    }
}
